package com.ruoyi.question.service.impl;

import com.ruoyi.question.domain.DtRanking;
import com.ruoyi.question.mapper.DtRankingMapper;
import com.ruoyi.question.service.IDtRankingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 排行榜Service业务层处理
 *
 * @author llc
 * @date 2025-01-11
 */
@Service
public class DtRankingServiceImpl implements IDtRankingService
{
    @Autowired
    private DtRankingMapper dtRankingMapper;

    /**
     * 使用用户id查询排行榜
     *
     * @param userId 用户id
     * @return 排行榜
     */
    @Override
    public DtRanking selectDtRankingByUserId(Long userId)
    {
        return dtRankingMapper.selectDtRankingByUserId(userId);
    }

    /**
     * 查询排行榜列表
     *
     * @param dtRanking 排行榜
     * @return 排行榜
     */
    @Override
    public List<DtRanking> selectDtRankingList(DtRanking dtRanking)
    {
        List<DtRanking> list = dtRankingMapper.selectDtRankingList(dtRanking);
        list.forEach(dtRanking1 -> {
            dtRanking1.setRank((long) (list.indexOf(dtRanking1)+1));
        });
        return list;
    }

    /**
     * 新增排行榜
     *
     * @param dtRanking 排行榜
     * @return 结果
     */
    @Override
    public int insertDtRanking(DtRanking dtRanking)
    {
        return dtRankingMapper.insertDtRanking(dtRanking);
    }

    /**
     * 修改排行榜
     *
     * @param dtRanking 排行榜
     * @return 结果
     */
    @Override
    public int updateDtRanking(DtRanking dtRanking)
    {
        DtRanking selectdtRanking = new DtRanking();
        selectdtRanking.setUserId(dtRanking.getUserId());
        List<DtRanking> dtRankingList = dtRankingMapper.selectDtRankingList(selectdtRanking);
        if(dtRankingList.size() == 1) {
            DtRanking newDtRanking = dtRankingList.get(0);
            newDtRanking.setCorrectCount(dtRanking.getCorrectCount()+newDtRanking.getCorrectCount());
            return dtRankingMapper.updateDtRanking(newDtRanking);
        }
        return dtRankingMapper.insertDtRanking(dtRanking);
    }

    /**
     * 批量删除排行榜
     *
     * @param ids 需要删除的排行榜主键
     * @return 结果
     */
    @Override
    public int deleteDtRankingByIds(Long[] ids)
    {
        return dtRankingMapper.deleteDtRankingByIds(ids);
    }

    /**
     * 删除排行榜信息
     *
     * @param id 排行榜主键
     * @return 结果
     */
    @Override
    public int deleteDtRankingById(Long id)
    {
        return dtRankingMapper.deleteDtRankingById(id);
    }
}
